package com.pink.component;

/**
 * Redis组件
 * * @author: 神月恭平
 * * @date: 2025/04/18 22:57
 */

import com.pink.entity.config.AppConfig;
import com.pink.entity.config.Constants;
import com.pink.entity.enums.DateTimePatternEnum;
import com.pink.entity.query.EmailQuery;
import com.pink.entity.query.VideoFileQuery;
import com.pink.entity.vo.response.SysSettingVo;
import com.pink.entity.vo.response.UploadingFileVo;
import com.pink.redis.RedisUtils;
import com.pink.utils.DateUtil;
import com.pink.utils.StringTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Component
@Slf4j
public class RedisComponent {

    // 注入RedisUtils
    @Resource
    private RedisUtils redisUtils;

    // 注入AppConfig
    @Resource
    private AppConfig appConfig;

    // 保存验证码
    public void saveCheckCode( String checkCodeKey,String code){
        // 将验证码存入Redis，并设置过期时间为1分钟
        redisUtils.setex(Constants.REDIS_KEY_CHECK_CODE_KEY + checkCodeKey,code, Constants.REDIS_KEY_EXPIRES_ONE_MIN * 3);
    }

    // 获取验证码
    public String getChckeCode(String checkCodeKey){
        // 从Redis中获取验证码
        String code = (String) redisUtils.get(Constants.REDIS_KEY_CHECK_CODE_KEY + checkCodeKey);
        // 如果验证码为空，则返回空字符串
        return code == null ? "" : code;
    }

    // 删除验证码
    public void delCheckCode(String checkCodeKey){
        // 从Redis中删除验证码
        redisUtils.delete(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey);
    }

    //获取邮箱验证码
    public EmailQuery getEmailQueue(String email) {
        return (EmailQuery) redisUtils.get(Constants.REDIS_KEY_EMAIL_CODE+email);
    }

    //设置邮箱验证码
    public EmailQuery getEmailQueue() {
        EmailQuery emailQuery = (EmailQuery) redisUtils.get(Constants.REDIS_KEY_EMAIL);
        delEmailQueue();
        return emailQuery;
    }

    //设置邮箱验证码
    public void setEmailQueue(EmailQuery emailQuery) {
        redisUtils.setex(Constants.REDIS_KEY_EMAIL,emailQuery, Constants.REDIS_KEY_EXPIRES_ONE_MIN * 3);
        redisUtils.setex(Constants.REDIS_KEY_EMAIL_CODE+emailQuery.getEmail(), emailQuery,Constants.REDIS_KEY_EXPIRES_ONE_MIN);
    }

    //删除邮箱验证码
    public void delEmailQueue() {
        redisUtils.delete(Constants.REDIS_KEY_EMAIL);
    }

    public void delEmailQueue(String email) {
        redisUtils.delete(Constants.REDIS_KEY_EMAIL_CODE+email);
    }

    //保存预上传信息
    public String svaPreVideoFileInfo(String userId,String fileName,Integer chunks){
        String uploadId = StringTool.getRandomString(Constants.LENGTH_15);
        UploadingFileVo fileDto = new UploadingFileVo();
        fileDto.setChunks(chunks);
        fileDto.setFileName(fileName);
        fileDto.setUploadId(uploadId);
        fileDto.setChunkIndex(0);
        String day = DateUtil.format(new Date(), DateTimePatternEnum.YYYYMMDD.getPattern());
        String filePath = day+"/"+userId+uploadId;
        fileDto.setFilePath(filePath);
        String folder = appConfig.getProjectFolder()+Constants.FILE_FOLDER+Constants.FILE_FOllDER_TEMP+filePath;
        File folderFile = new File(folder);
        if(!folderFile.exists()){
            folderFile.mkdirs();
        }
        redisUtils.setex(Constants.REDIS_KEY_UPLOADING_FILE + userId + uploadId,fileDto, Constants.REDIS_KEY_EXPIRES_ONE_DAY);
        return uploadId;
    }

    //获取预上传信息
    public UploadingFileVo getPreVideoFileInfo(Integer userId,String uploadId){
        return (UploadingFileVo) redisUtils.get(Constants.REDIS_KEY_UPLOADING_FILE + userId + uploadId);
    }

    //获取预上传信息
    public UploadingFileVo UploadingFileVo(Integer userId, @NotNull String uploadId) {
        return (UploadingFileVo) redisUtils.get(Constants.REDIS_KEY_UPLOADING_FILE + userId + uploadId);
    }

    public SysSettingVo getSysSettingVo() {
        SysSettingVo sysSettingDto = (SysSettingVo) redisUtils.get(Constants.REDIS_KEY_SYS_SETTING);
        if(sysSettingDto == null){
            sysSettingDto = new SysSettingVo();
        }
        return sysSettingDto;
    }

    //上传信息保存
    public void updateVideoFileInfo(Integer userId, UploadingFileVo fileDto) {
        redisUtils.setex(Constants.REDIS_KEY_UPLOADING_FILE + userId + fileDto.getUploadId(),fileDto, Constants.REDIS_KEY_EXPIRES_ONE_DAY);
    }

    //视频信息处理流
    public void addFile2TransferQueue(VideoFileQuery videoFileQuery) {
        List<VideoFileQuery> videoFileQuerList = List.of(videoFileQuery);
        redisUtils.lpushAll(Constants.REDIS_KEY_QUEUE_TRANSFER, videoFileQuerList, 0);
    }

    //获取视频信息处理流
    public VideoFileQuery getFileFromTransferQueue() {
        return (VideoFileQuery) redisUtils.rpop(Constants.REDIS_KEY_QUEUE_TRANSFER);
    }

    public void delVideoFileInfo(Integer userId, String uploadId) {
        redisUtils.delete(Constants.REDIS_KEY_UPLOADING_FILE + userId + uploadId);
    }

    public void logoutUser(String uuid) {
        redisUtils.delete(Constants.JWT_BLACK_LIST + uuid);
    }
}
